<?php

abstract class Mediad2_Controller_Action extends Zend_Controller_Action
{
	/**
	 * vars
	 */
	//变量
	protected $helper;								//视图帮助类
	protected $pageview;							//视图
	protected $viewName;							//视图名称
	protected $session;								//Session
	protected $result;								//result
	protected $httpdata;							//httpdata
	protected $ip;									//IP
	
	/**
	 * functions object
	 *
	 * @var Mediad2_Utility_Functions
	 */
	protected $functions;
	
	/**
	 * 初始化
	 * @see Zend_Controller_Action::init()
	 */
	public function init()
	{	
		header("Content-type:text/html;charset=utf-8");
		$registry = Zend_Registry::getInstance();
		$this->pageview = $registry->view;
		$this->helper 	= new Mediad2_Utility_SmartyHelper ();
		$this->session 	= new Zend_Session_Namespace('Mediad2');
		$this->result 	= new Mediad2_Model_Result();
		$this->httpdata = $this->getHttpDataFromRequest();		//获取httpdata
		$this->functions = new Mediad2_Utility_Functions();		//获取functions
		$this->ip = getenv("REMOTE_ADDR");
	}
	
	/**
	 * 获取session中的var
	 * @param string $var
	 * @return string|NULL
	 */
	protected function getSessionVar($var)
	{
		if ($this->session->$var == null) {
			return null;
		} else {
			return $this->session->$var;
		}
	}
	
	/**
	 * 设置session中的var
	 * @param string $var
	 * @param mixed $data
	 */
	protected function setSessionVar($var, $data)
	{
		$this->session->$var = $data;
	}
	
	/**
	 * 判断是否设置session中的var
	 * @param string $var
	 * @return boolean
	 */
	protected function issetSessionVar($var)
	{
		return $this->session->__isset($var);
	}
	
	/**
	 * set result
	 *
	 * @param string $result
	 * @throws Exception
	 * @return Mediad2_Service_Abstract
	 */
	protected function setResult($result)
	{
		if (is_string($result)) {
			$result = new $result();
		}
	
		if (!($result instanceof Mediad2_Model_Result)) {
			throw new Mediad2_Service_Exception('缺少类Mediad2_Model_Result!');
		}
		$this->result = $result;
		return $this;
	}
	
	/**
	 * get result
	 *
	 * @return Mediad2_Model_Result
	 */
	protected function getResult ()
	{
		if (null === $this->result) {
			$this->setResult('Mediad2_Model_Result');
		}
		return $this->result;
	}
	
	/**
	 * return $result
	 *
	 * @param string $code
	 * @param string $message
	 * @param string $data
	 * @return Mediad2_Model_Result
	 */
	protected function returnResult($code, $message = null, $data = null)
	{
		$result = new Mediad2_Model_Result();
	
		if (!isset($message)) {
			switch ($code) {
				case Mediad2_Model_Result::CODE_SUCCESS:
					$message = Mediad2_Model_Result::MESSAGE_SUCCESS;
					break;
				case Mediad2_Model_Result::CODE_FAILURE:
					$message = Mediad2_Model_Result::MESSAGE_FAILURE;
					break;
				default:
					$message = '';
				break;
			}
		}
		if (!isset($data)) {
			$data = '';
		}
	
		$result->setCode($code);
		$result->setMessage($message);
		$result->setData($data);
	
		$this->setResult($result);
		return $this->result;
	}
	
	/**
	 * cookie操作
	 * 
	 * @param string|int $status
	 * @param string $key
	 * @param string $value
	 * @return string|boolean
	 */
	protected function cookieMethod($operate, $key, $value = '')
	{
		$result = null;
		switch ($operate) {
			case "save":
			case "1":
				setcookie($key, $value, time()+3600*24*30);
				$result = true;
				break;
				
			case "get":
			case "2":
				if (isset($_COOKIE[$key])) {
					$result = $_COOKIE[$key];
				} else {
					setcookie($key, $value, time()+3600*24*30); 
					$result = $value;
				}
				break;
				
			case "del":
			case "3":
				setcookie($key,$value,time()-3600);
				$result = true;
				break;
			
			default:
				$result = false;
				break;
		}
		
		return $result;
	}
	
	/**
	 * result 编码
	 * 
	 * @param Mediad2_Model_Result $result
	 * @return string JSON encoded object
	 */
	protected function encodeResult(Mediad2_Model_Result $result) 
	{
		return Zend_Json::encode($result->toArray());
	}
	
	/**
	 * 得到http数据
	 * 
	 * @return array include _POST && _GET
	 */
	protected function getHttpDataFromRequest()
	{
		$data = array();
		$postdata = $this->getRequest()->getPost();
		foreach ($postdata as $k => $v) {
			$v = addslashes($v);
			$v = str_replace(array('%'), array('\%'), $v);
			$data[$k] = trim($v);
		}
		$getdata = $this->getRequest()->getQuery();
		foreach ($getdata as $k => $v) {
			$v = addslashes($v);
			$v = str_replace(array('%'), array('\%'), $v);
			$data[$k] = trim($v);
		}
		return $data;
	}
	
	/**
	 * handle exception
	 *
	 * @param Zend_Exception $e
	 * @return Mediad2_Service_Abstract
	 */
	protected function handleException(Zend_Exception $e, $debug = false)
	{
		if ($debug) {
			var_dump($e->getMessage());
			return $this;
		} else {
			//出错处理--log
			$registry = Zend_Registry::getInstance ();
			if ($registry->isRegistered('log')) {
				$log = $registry->get('log');
				$log->crit($e->getMessage(), $e);
			}
			//出错处理--邮件
			$em = new Mediad2_Service_Util_Email();
			$em->sendException($e->getMessage());
			return $this;
		}
	}
	
	/**
	 * echo 错误信息，然后退出
	 * @param string $message
	 */
	protected function handleErrorAndExit($message)
	{
		$result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,$message);
		echo $this->encodeResult($result);
		exit(0);
	}
	
	/**
	 * array to xml
	 *
	 * @param array $array
	 * @return string $xml
	 */
	protected function array2xml($array)
	{
		$fn = new Mediad2_Utility_Functions();
		return $fn->array2xml($array);
	}
	
	/**
	 * @author vincent 20130720
	 * @when funciton complete, printer the json data
	 * @param string $flagXml
	 * @return string
	 */
	protected function complete($flagXml)
	{
		if ($flagXml == '1') {
			echo sprintf("<response>%s</response>",$this->array2xml ( $this->result->toArray() ));
		} else {
			echo $this->encodeResult($this->result);
		}
	}

}
